﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.DBUtility;
using MeTalker.Aide.Model;

namespace MeTalker.Aide.Forms
{
    internal partial class frmAutoReplyEdit : MeForm
    {
        AnswerMod answer;
        public frmAutoReplyEdit()
        {
            InitializeComponent();
        }
        public frmAutoReplyEdit(AnswerMod answer)
            : this()
        {
            this.answer = AccessHelper.GetProblem(answer.Id);
            txtAnswer.Text = this.answer.Answer;
            txtQuestion.Text = this.answer.Problem;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtQuestion.Text))
            {
                MeMessageBox.ShowInformation("问题不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(txtAnswer.Text))
            {
                MeMessageBox.ShowInformation("答案不能为空!");
                return;
            }
            int effect = 0;
            if (answer != null)
            {
                answer.Answer = txtAnswer.Text;
                answer.Problem = txtQuestion.Text;
                effect = AccessHelper.UpdateProblem(answer);
            }
            else
            {
                Answer = new AnswerMod() { Problem = txtQuestion.Text, Answer = txtAnswer.Text };
                effect = AccessHelper.InsertProblem(Answer);
            }
            if (effect > 0)
            {
                DialogResult = DialogResult.Yes;
                AccessHelper.GetProblemList();
            }
            this.Close();
        }

        public AnswerMod Answer{ get; private set; }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            UtilHelper.RunBrowser(UtilHelper.HELP_BASE_URL + "58.html");
        }
    }
}